//
//  LBSingChooseTableView.m
//  V-Learn
//
//  Created by Apple on 2019/2/22.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBSingChooseTableView.h"
#import "LBShuangyuTableViewCell.h"
#import "UIView+WHC_AutoLayout.h"
#import "UITableViewCell+WHC_AutoHeightForCell.h"
#import "WKSubjectOptionVOListModel.h"
#import "ZSSaveTools.h"
#import "TadayExamTableViewCell.h"
#define rgba(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


#define rgb(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define HeaderHeight 50
#define CellHeight 50
#define Ktimer 10
#define FONT(a)  [UIFont fontWithName:@"Heiti SC" size:a]
@interface LBSingChooseTableView (){
    
    /**
     *  题目
     */
    UILabel *titleLabel;
    
    NSArray *_chooseArr;
    
    NSArray *_noChooseArr;
    
    BOOL _isHistoryExam;
    
    // TadayExamTableViewCell  * cell;
}

@property(nonatomic,copy)NSString *string;

@property(nonatomic,assign)NSInteger dex;

@property(nonatomic,strong)NSString *practiceStr;
@property(nonatomic,strong)NSString *practiceStr2;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *countLab2;


@end

@implementation LBSingChooseTableView
+(LBSingChooseTableView *)ShareTableWithFrame:(CGRect)frame{
    
    LBSingChooseTableView *table = [[LBSingChooseTableView alloc]initWithViewFrame:frame];
    table.backgroundColor = rgb(242,242,242);//[UIColor whiteColor];//表格背景颜色
    return table;
}


-(instancetype)initWithViewFrame:(CGRect)frame{
    self = [super init];
    if(self){
        self.frame = frame;
        [self CreateTable];
    }
    return self;
}


-(UIView *)createHeadView:(NSString *)title{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(38, 0, SCREEN_WIDTH-76, 120)];
        _headView.backgroundColor = rgb(242,242,242);//[UIColor whiteColor];//头部视频
//        _headView.layer.cornerRadius = 13.5;
//        _headView.layer.masksToBounds = YES;
//        _headView.backgroundColor= [UIColor redColor];
        
        UIImageView *bgImageView = [[UIImageView alloc] init];
        bgImageView.image = [UIImage imageNamed:@"wk_shenqingtop_card"];
        [_headView addSubview:bgImageView];
        bgImageView.frame = CGRectMake(0, 10, SCREEN_WIDTH-76, 50);
        
        UIView *bgView = [UIView new];
        
        bgView.frame = CGRectMake(0, 50, SCREEN_WIDTH-76, 70);
        bgView.backgroundColor = [UIColor whiteColor];
        [_headView addSubview:bgView];
        titleLabel = [[UILabel alloc]init];
        titleLabel.text = title;
        titleLabel.numberOfLines = 0;
        titleLabel.font = FONT(18);//显示字体太大 当前是 psd 大小 24
        titleLabel.textColor = rgb(40,40,40);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [bgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
//            make.centerX.mas_equalTo(_headView);
            make.top.equalTo(self.headView.mas_top).offset = 60;
            make.left.mas_equalTo(3);
            make.right.mas_equalTo(-3);
        }];
        
        
        
    }
    _MyTable.scrollEnabled = NO;
    _MyTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _MyTable.tableHeaderView = _headView;
    
    
    return _headView;
}

-(UIView *)createFootViewWithAnswer:(UILabel *)answerLabel andWithOtherAnswer:(UILabel *)otherLabel AndWith:(BOOL)isFlast andWithCorrent:(BOOL)isCorrent andResolve:(UILabel *)resolveLabel{
    
    UILabel *resloveLabel = [[UILabel alloc]init];
    
    
    if (isFlast) {
        
        if (!_footView) {
            _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
            // _footView.backgroundColor = [UIColor yellowColor];
            UILabel *myLabel  = [[UILabel alloc]init];
            myLabel.font = FONT(15);
            
            //回答正确
            if (isCorrent) {
                myLabel.text = [NSString stringWithFormat:@"正确答案是%@,你的答案是%@,回答正确",answerLabel.text,otherLabel.text];
                
            }else{
                
                myLabel.text = [NSString stringWithFormat:@"正确答案是%@,你的答案是%@,回答错误",answerLabel.text,otherLabel.text];
                
            }
            
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:myLabel.text];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(5,1)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(12,1)];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(16,2)];
            myLabel.attributedText = str;
            [_footView addSubview:myLabel];
            
            [myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_footView.mas_top).offset = 15;
                make.left.equalTo(_footView.mas_left).offset = 10;
            }];
            /**
             *  解析
             */
            resloveLabel.text = @"解析";
            resloveLabel.textColor = [UIColor lightGrayColor];
            [_footView addSubview:resloveLabel];
            
            
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = [UIColor grayColor];
            [_footView addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(_footView);
                make.height.equalTo(@1);
                make.top.equalTo(myLabel.mas_bottom).offset = 10;
            }];
            [resloveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_footView.mas_left).offset = 10;
                make.top.equalTo(lineView.mas_bottom).offset = 10;
            }];
            
            UILabel *resloveDetailLabel = [[UILabel alloc]init];
            resloveDetailLabel.text = resolveLabel.text;
            resloveDetailLabel.textColor = [UIColor darkGrayColor];
            resloveDetailLabel.numberOfLines = 0;
            [_footView addSubview:resloveDetailLabel];
            [resloveDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(_footView.mas_right).offset = -10;
                make.left.equalTo(_footView.mas_left).offset = 10;
                make.top.equalTo(resloveLabel.mas_bottom).offset = 5;
            }];
            
        }
        
        
        //        _MyTable.tableFooterView = _footView;
        
        
        
    }else{
        
        return nil;
    }
    
    
    return _footView;
}


-(void)CreateTable{
    
    
    
    
    
    //先初始化赋值
    self.dex = 100;
    
//    _chooseArr = @[@"a-_sel",@"b-_sel",@"c-_sel",@"d-_sel"];
//    _noChooseArr = @[@"a",@"b",@"c",@"d"];
//
    _MyTable = [[UITableView alloc]initWithFrame:CGRectMake(38, 65, SCREEN_WIDTH-76, ScreenH-181-LBNAVBAR_HEIGHT-65)];
    _MyTable.dataSource = self;
    _MyTable.delegate = self;
    //    _MyTable.tableFooterView = _footView;
    _MyTable.tableHeaderView = _headView;
    
    _MyTable.separatorStyle = NO;
    //    _MyTable.backgroundColor = [UIColor clearColor];
    
    //    UIImageView *imageView = [[UIImageView alloc] init];
    //    imageView.image = [UIImage imageNamed:@""];
    //    imageView.frame = _MyTable.frame;
    //    [self addSubview:imageView];
    
    
    _MyTable.backgroundColor = [UIColor clearColor];
    
    UIView *redView = [UIView new];
    
    redView.frame = CGRectMake(38, 65, SCREEN_WIDTH-76, ScreenH-181-LBNAVBAR_HEIGHT-66);
    redView.backgroundColor = [UIColor whiteColor];
    redView.layer.cornerRadius = 13.5;
    _MyTable.layer.cornerRadius = 13.5;
    _MyTable.layer.masksToBounds = YES;
    //如果 为ture 阴影想过出不来
    redView.layer.masksToBounds = false;
    
    [self addSubview:redView];
    [self addShadowToView:redView withColor:rgba(233,84,18,0.2)];
    
    
    [self addSubview:_MyTable];
    
    
    //[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(UnableIsHistoryExam:) name:@"UnableIsHistoryExam" object:nil];
    
    
    
    
}

#pragma mark - 首页顶部 添加四边阴影效果
- (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
    // 阴影颜色1
    theView.layer.shadowColor = theColor.CGColor;
    // 阴影偏移，默认(0, -3)
    theView.layer.shadowOffset = CGSizeMake(0,0);
    // 阴影透明度，默认0
    theView.layer.shadowOpacity = 0.5;
    // 阴影半径，默认3
    theView.layer.shadowRadius = 5;
    
}
- (void)dealloc
{
    //  HQLAppLog(@"移除通知");
    
    //    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyChooseAnswer" object:nil];
    //    [ZSSaveTools removeObject:@"answerTagIndex"];
}

-(void)UnableIsHistoryExam:(NSNotification *)notion{

    _isHistoryExam = YES;

}


#pragma UITableViewDelegate - UITableViewDataSource


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat h = [LBShuangyuTableViewCell whc_CellHeightForIndexPath:indexPath tableView:_MyTable];
//    NSLog(@"%f",h);
    return h+20;
    
//    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LBShuangyuTableViewCell  * cell =[tableView dequeueReusableCellWithIdentifier:@"LBShuangyuTableViewCell"];
    
    if (!cell) {
        cell = [[LBShuangyuTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LBShuangyuTableViewCell"];
    }
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    WKSubjectOptionVOListModel *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.connentLabel.text = model.optionName;
    [cell.selectBtn setTitle:model.optionName forState:UIControlStateNormal];

    
    NSDictionary *answerDict = [ZSSaveTools getUserInfoValueForKey:@"answerDict"];
    
    NSString *string = [ZSSaveTools getUserPhoneAndPwdValueForKey:@"string"];
    
    //    NSString *testString = [ZSSaveTools getUserPhoneAndPwdValueForKey:@"testString"];
    NSString *tagIndex = [answerDict objectForKey:string];
    
    self.practiceStr = tagIndex;
    
    
    //  HQLAppLog(@"我是单选%@-----valve答案:%@---key题号:%@",answerDict,tagIndex,string);
//    NSLog(@"我是单选%@-----valve答案:%@---key题号:%@",answerDict,tagIndex,string);
    
    
    if (tagIndex != nil) {
        
        if (indexPath.row == [tagIndex integerValue]-1) {
            
            [cell UpdateCellWithState:YES];
        }
        
    }else {
        [cell UpdateCellWithState:NO];
    }
    if (self.model1.selectIndex == indexPath.row) {
        [cell UpdateCellWithState:YES];
    }else {
        [cell UpdateCellWithState:NO];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /**
     *  历史模考不能再次选择答案
     */
    //NSString *historyExam = [ZSSaveTools getUserPhoneAndPwdValueForKey:@"historyExam"];
    if (self.dontSelectCell) {
        
        return;
    }
    
    if (self.practiceStr.length > 0 && self.preiceDontSelect) {
        
        return;
    }
    
    
    
    if (_currentSelectIndex!=nil&&_currentSelectIndex != indexPath) {
        LBShuangyuTableViewCell  *cell = [tableView cellForRowAtIndexPath:_currentSelectIndex];
        [cell UpdateCellWithState:NO];
    }
    
    WKSubjectOptionVOListModel *model = [self.dataArr objectAtIndex:indexPath.row];
    
    LBShuangyuTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    _currentSelectIndex = indexPath;
    self.model1.selectIndex = indexPath.row;
    [self.MyTable reloadData];
    //    _block(cell.connentLabel.text,indexPath);
    _block(cell.connentLabel.text,indexPath,model.optionId,model.score);
}

-(void)ReloadData{
    [self.MyTable reloadData];
}
@end
